package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 检查项服务
 */
@Service(interfaceClass = CheckItemService.class)//如果服务有事务，必须指定interfaceClass属性，否则无法注册到注册中心
@Transactional
public class CheckItemServiceImpl implements  CheckItemService {

    @Autowired
    private CheckItemDao checkIteamDao;

    /**
     * 检查项编辑
     * @param checkItem
     */
    @Override
    public void edit(CheckItem checkItem) {
        checkIteamDao.edit(checkItem);
    }

    /**
     * 数据回显
     * @param id
     * @return
     */
    @Override
    public CheckItem findById(Integer id) {
        return checkIteamDao.findById(id);
    }

    /**
     * 根据Id删除检查项
     * @param id
     */
    @Override
    public void delete(Integer id) {
         //查询当前检查项和检查组是否关联
        long count = checkIteamDao.findCountByCheckItemId(id);
        if (count > 0 ){
            // 已关联,不能删除
            new RuntimeException();
        }
        checkIteamDao.deleteById(id);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //基于mybatis分页插件 实现分页
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckItem> checkItems = checkIteamDao.selectByCondition(queryPageBean.getQueryString());
        return new PageResult(checkItems.getTotal(),checkItems.getResult());
    }

    /**
     * 查询全部
     *
     * @return
     */
    @Override
    public List<CheckItem> findAll() {
        return checkIteamDao.findAll();
    }

    /**
     * 检查项增加
     *
     * @param checkItem
     */
    @Override
    public void add(CheckItem checkItem) {
        checkIteamDao.add(checkItem);
    }

}
